var SharedData = {};
function addFixedPackege(){
	waitingDialog({});
	$("[id=addtableFixed]").attr("style","display:;");
	$("[id=addtableFixed]").load("/adminconsole/PriceManager/Fixed/addfixedpackage.jsp",function(responseText, textStatus, XMLHttpRequest) {
		$("[id=tableFixed]").attr("style","display:none");
		$("#suggest").html(addpackage);
		$("[name=save]").attr("onclick","addfixedpackage()");
		$("[name=cancel]").attr("onclick","cancelFixedPackege()");
		loadAddFixpackageData();
		callDatePicker();
	});
}
function editFixedPackege(id){
	$("[id=addtableFixed]").attr("style","display:;");
	$("[id=addtableFixed]").load("/adminconsole/PriceManager/Fixed/addfixedpackage.jsp",function(responseText, textStatus, XMLHttpRequest) {
		$("[id=tableFixed]").attr("style","display:none");
		$("#suggest").html(editpackage);
		loadAddFixpackageData(id);
		callDatePicker();
	});
}
function loadAddFixpackageData(id){
	var data = {func : "getfixpagedata",id:id};
	var url = "/fixedpackageservice";
	simpleservice(url,data,fillData);
}
function fillData(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	SharedData.DateFormat = objArray[0]['dateFormatString'];
	SharedData.Guesttype = objArray[0]['guesttype'];
	SharedData.Roomtype = objArray[0]['roomtype'];
	showGuestType();
	showRoomType();
	if(objArray[1]!=null)
		renderEditFrom(objArray[1]);
	closeWaitingDialog();
}

function cancelFixedPackege(type){
	if(type!="archived"){
		$("[id=addtableFixed]").attr("style","display:none;");
		$("[id=tableFixed]").attr("style","display:;");
	}else{
		$("[id=addtableFixedArchived]").attr("style","display:none;");
		$("[id=tableFixedArchived]").attr("style","display:;");
	}
}

function showGuestType(){
	var txt = "<tr><td ><input name=\"gtype\" type=\"checkbox\" value=\"all\" " +
			"onclick=\"checkAll()\"/>All</td>";
	var guesttype = SharedData.Guesttype;
	for(var i=0;i<guesttype.length;i++){
		if((i+1)%3!=0){
			txt +="<td ><input name=\"gtype\" type=\"checkbox\" " +
			"value=\""+guesttype[i]["id"]+"\" onclick=\"nodeCheck("+(i+1)+")\"/>"+
			guesttype[i]["title"]+"</td>";
		}else{
			txt +="</tr><tr><td ><input name=\"gtype\" type=\"checkbox\" " +
			"value=\""+guesttype[i]["id"]+"\" onclick=\"nodeCheck("+(i+1)+")\"/>"+
			guesttype[i]["title"]+"</td>";
		}
	}
	txt+= "</tr>";
	$("[id*=gtList]:visible").append(txt);
}

function showRoomType(){
	var txt = "";
	var roomtype = SharedData.Roomtype;
	for(var i=0;i<roomtype.length;i++){
			txt +="<tr name=\"rttable\" roomtypeid=\""+roomtype[i]["id"]+"\">"+
                	"<td><span>"+roomtype[i]["shortName"]+"</span></td>"+
                    "<td><span>"+roomtype[i]["StandardOccupancy"]+"</span></td>"+
                    "<td><span>"+roomtype[i]["MaxOccupancy"]+"</span></td>"+
                    "<td><span>"+roomtype[i]["basePrice"]+"</span></td>"+
                    "<td><span>"+roomtype[i]["upchargeperson"]+"</span></td>"+
                    "<td><span>"+roomtype[i]["extraBedPrice"]+"</span></td>"+
                    "<td><input class=\"inputtextright\" name=\"ppn\" type=\"text\" size=\"8\" value=\""+
                    roomtype[i]["basePrice"]+"\">&nbsp;"+xSymbol+"</td>"+
                    "<td><input class=\"inputtextright\" name=\"ucpp\" type=\"text\" size=\"8\" value=\""+
                    roomtype[i]["upchargeperson"]+"\">&nbsp;"+xSymbol+"</td>"+
                    "<td><input class=\"inputtextright\" name=\"exbed\" type=\"text\" size=\"8\" value=\""+
                    roomtype[i]["extraBedPrice"]+"\">&nbsp;"+xSymbol+"</td>"+
                "</tr>";	
	}
	$("[id*=rtList]:visible").append(txt);
}

function checkAll(){
	var ischeck = $("#gtList td [name=gtype]").first().is(":checked");
		for(var i=0;i<$("#gtList td [name=gtype]").size();i++){
			$($('input[name=gtype]')[i]).prop('checked', ischeck);
		}
}

function nodeCheck(i){
	if($("#gtList td [name=gtype]").first().is(":checked") && !$($("#gtList td [name=gtype]")[i]).is(":checked")){
		$($("#gtList td [name=gtype]").first()).attr('checked', false);
	}
}

function callDatePicker(){
	$(function() {
		$("#from").datepicker({
	    	dateFormat: 'yy-mm-dd',
	    	minDate: new Date(getServerDateTime()),
	    	onSelect: function(DATE)
			{
				$('#from').val($.datepicker.parseDate("yy-mm-dd",DATE).getFormatedString());
				$('#from').attr('date',DATE);
				$( "#to").datepicker( "option", "minDate", DATE);
			}
	    });
	  });
	$(function() {
	    $( "#to" ).datepicker({
	    	dateFormat: 'yy-mm-dd',
	    	onSelect: function(DATE)
			{
				$('#to').val($.datepicker.parseDate("yy-mm-dd",DATE).getFormatedString());
				$('#to').attr('date',DATE);
			}
	    });
	  });
}

function addfixedpackage(typepage){
	waitingDialog({});
	if(typepage=="archived"){
		var line = $("[name=maxEntriesarchived]").val();
		var pagePointer = $("[name=selectpagearchived]").val();
	}else{
		var line = $("[name=maxEntries]").val();
		var pagePointer = $("[name=selectpage]").val();
		if(pagePointer==null)
			pagePointer = 1;
	}
	var id =$("[id*=packagedetailtable]:visible").attr("fixedId");
	if($("[id*=packagename]:visible").val() == "" ||$("[id*=longofstay]:visible").val() == "" 
		|| $("[id*=description]visible").val() == ""){
		simpleDialog("please input data");
		return;
	}
	if(!choseGuestType()){
		simpleDialog("please input data");
		return;
	}
	var packagename = $("[id*=packagename]:visible").val();
	var longofstay = $("[id*=longofstay]:visible").val();
	var description = $("[id*=description]:visible").val();
	if($("[name=lengthofstay]:checked").val() == "forever"){
		var isforever = true;
	}else{
		var isforever = false;
		if($("[id*=from]:visible").val() == "" || $("[id*=to]:visible").val() == ""){
			simpleDialog("please input data");
			return;
		}else{
			var from = $("[id*=from]:visible").attr("date");
			var to = $("[id*=to]:visible").attr("date");
		}
	}
	var guesttype = "";
	for(var i=1;i<$("[id*=gtList] td [name=gtype]:visible").size();i++){
		if($($("[id*=gtList] td [name=gtype]:visible")[i]).is(":checked")){
			guesttype += $($("[id*=gtList] td [name=gtype]:visible")[i]).val() +",";
		}
	}
	guesttype = guesttype.substring(0,guesttype.length-1);
	var status = $("[name=gtype_on]:visible").is(":checked");
	var pricedata = "";
	for(var i = 0;i<$("[id*=rtList] tr[name=rttable]:visible").size();i++){
		pricedata += $($("[id*=rtList] tr[name=rttable]:visible")[i]).attr("roomtypeid")+","+$($("[name=ppn]:visible")[i]).val() +","+$($("[name=ucpp]:visible")[i]).val()+","+
		$($("[name=exbed]:visible")[i]).val()+"-";
	}
	pricedata = pricedata.substring(0,pricedata.length-1);
	var url = "/fixedpackageservice";
	var func = "addfixedpackage";
	var type = "";
	if(id!=null)
		type = "edit";
	else
		type = "add";
	var data = { func : func,
			id : id,
			packagename : packagename,
			longofstay : longofstay ,
			description : description,
			isforever: isforever,
			from:from,
			to:to,
			guesttype:guesttype,
			status:status,
			pricedata:pricedata,
			line: line,
			pagePointer: pagePointer,
			offset: (line*pagePointer)-line,
		}
	if(typepage=="archived")
		simpleserviceWithArgs(url,data,showArchivedFixedPackageList,type);
	else
		simpleserviceWithArgs(url,data,showFixedPackageList,type);
}

function choseGuestType(){
	var isChose = false;
	for(var i=0;i<$("#gtList td [name=gtype]").size();i++){
		if($($("#gtList td [name=gtype]")[i]).is(":checked")){
			isChose = true;
			break;
		}
	}
	return isChose;
}

function swicth(name){
	if(name == "gtype_on"){
		$("[name=gtype_off]").attr('checked', false);
	}else if(name == "gtype_off"){
		$("[name=gtype_on]").attr('checked', false);
	}else if(name == "true"){
		$("#datefrom").val();
		$("#datefrom").val();
		$("#datefrom").attr("disabled",true);
		$("#dateto").attr("disabled",true);
	}else if(name == "false"){
		$("#datefrom").attr("disabled",false);
		$("#dateto").attr("disabled",false);
	}
}

function checklengthofstay(){
	if($("[name=lengthofstay]:checked").val() != "forever"){
		$("#from").attr("disabled",false);
		$("#to").attr("disabled",false);
	}else{
		$("#from").attr("disabled",true);
		$("#to").attr("disabled",true);
	}
}

var successAddTask = null;
var successEditTask = null;
function showFixedPackage(id,type){
	var objArray = new Array();
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	if(type=="archived"||type=="edittime"){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}
	if(id == null){
		if($("[name=maxEntries]").val() == null){
			var line = 10;
		}else{
			var line = $("[name=maxEntries]").val();
		}
		if($("[name=selectpage]").val() == null){
			var pagePointer = 1; 
		}else{
			var pagePointer = $("[name=selectpage]").val(); 
		}
	}else if(id == "next"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())+1;
	}else if(id == "previous"){
		var line = $("[name=maxEntries]").val();
		var pagePointer = parseInt($("[name=selectpage]").val())-1;		
	}
	var link = "/fixedpackageservice";
	var data = { func: "getallfixedpackage",
				offset: (line*pagePointer)-line,
				line: line,
				pagePointer: pagePointer
					}
	simpleservice(link,data,showFixedPackageList);
}

function showFixedPackageList(data,type){
	if(type=="add"){
		if(successAddTask!=null)
			clearTimeout(successAddTask);
		$("#added").hide();
		$("#added").show();
		$("#addsuccess").html($("#packagename").val());
		successAddTask = setTimeout(function(){$("#added").hide(1000);}, 5000);
		$("[id=addtableFixed]").attr("style","display:none;");
		$("[id=tableFixed]").attr("style","display:;");
	}else if(type=="edit"){
		if(successEditTask!=null)
			clearTimeout(successEditTask);
		$("#edited").hide();
		$("#edited").show();
		$("#editsuccess").html($("#packagename").val());
		successEditTask = setTimeout(function(){$("#edited").hide(1000);}, 5000);
		$("[id=addtableFixed]").attr("style","display:none;");
		$("[id=tableFixed]").attr("style","display:;");
	}
	cleartoggleSelect();
	var objArray2 = new Array();
	objArray2 = JSON.parse(data);
	$("#counterFixed").html(objArray2[0]);
	var checkData = objArray2[0];
	$("#counterArchivedFixed").html(objArray2[5]);
	var checkLogOut = checkSessionAdmin(objArray2);
	if(checkLogOut)
		return;
	var sumtext = "";
	var text = "";
	var pageAmount = Math.ceil(objArray2[0]/objArray2[2]);
	var line = objArray2[2];
	var pagePointer = objArray2[1];
	var objArray = objArray2[3];
	dateFormatString =  objArray2[4];
	/*if(objArray.length==0){
		$("#showList").append("<tr><td colspan='8' style='text-align:center;'>Data Empty</td></tr>");
		return;
	}*/
	for(var i = 0;i< objArray.length+1;i++){
			if(i != objArray.length ){
				for(var x =0;x<objArray[i]["packagepriceList"].length ;x++){
					if(checkData==0)
						continue;
					if(objArray[i]["isForever"]){
						var packaetime = "Forever";
						var tdpackage = "<td  align=\"center\" rowspan=\""+objArray[i]["packagepriceList"].length+"\"><a href=\"#\" onclick=\"editpackagetime($(this),"+objArray[i]['id']+") \">"+packaetime+"</td>";
					}else{
						var packagetime = new Date(objArray[i]["from"]).getFormatedString()+
						" - "+new Date(objArray[i]["to"]).getFormatedString();
						if(new Date(objArray[i]["to"])<=getServerDateTime()){
							var tdpackage = "<td  align=\"center\" rowspan=\""+objArray[i]["packagepriceList"].length+"\"><span class='hide-text'>"+packagetime+"</span></td>";
						}else{
							var tdpackage = "<td  align=\"center\" rowspan=\""+objArray[i]["packagepriceList"].length+"\"><a href=\"#\" onclick=\"editpackagetime($(this),"+objArray[i]['id']+") \">"+packagetime+"</td>";
						}
					}
					if(x == 0){
						var check = "";
						if(objArray[i]['status']=="true"||objArray[i]['status']==true){
							check = "checked";
						}else{
							check = "";
						}
						text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
						"<td align=\"center\" rowspan=\""+objArray[i]["packagepriceList"].length+"\"> "+(i+1)+" </td>"+
						"<td rowspan=\""+objArray[i]["packagepriceList"].length+"\"><input type=\"checkbox\" id=\"id_"+i+"\" name=\"ids[]\" value=\""+objArray[i]['id']+"\"> </td>"+
						"<td align=\"left\" rowspan=\""+objArray[i]["packagepriceList"].length+"\">"+objArray[i]['packageName']+" </td>"+
						"<td align=\"left\">"+objArray[i]["packagepriceList"][x]["roomtype"]["shortName"]+" </td>"+
			            "<td align=\"left\">"+
						"<span>"+objArray[i]["packagepriceList"][x]["priceperNight"]+"</span>"+
						"</td> "+
							tdpackage+
						"<td align=\"center\" rowspan=\""+objArray[i]["packagepriceList"].length+"\"><span class='link' onclick='editFixedPackege("+objArray[i]['id']+");'>"+console_btn_edit+"</sapn> " +
						"</td>" +
						"<td align=\"center\" rowspan=\""+objArray[i]["packagepriceList"].length+"\">" +
							"<div id='itoggle'><input type='checkbox' id='"+objArray[i]['id']+"' "+check+" /></div>"+
						"</td>" +
						"</tr>";
					}else{
						text = "<tr id=\"row_"+objArray[i]['id']+"\" style=\"border: 0px;\"> " +
						"<td align=\"left\">"+objArray[i]["packagepriceList"][x]["roomtype"]["shortName"]+" </td>"+
			            "<td align=\"left\">"+
						"<span>"+objArray[i]["packagepriceList"][x]["priceperNight"]+"</span>"+
						"</td> "+
						"</tr>";
					}
					sumtext += text;
				}
			}else{
				text = "<tr>"+
				"<td colspan=\"13\" style=\"padding:0;\">"+
				"<table class=\"tablefoot\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
				"<tbody>"+
					"<tr>"+
						"<td width=\"33%\" align=\"left\">"+foor_page+"&nbsp;"+
							"<select name=\"selectpage\" onchange=\"showFixedPackage()\">"+
							"</select>&nbsp;"+foor_of+"&nbsp;<span id=\"pageAmount\">1</span>"+
						"</td>"+
						"<td width=\"31%\" align=\"center\">"+foor_view+
							"<select name=\"maxEntries\" onchange=\"showFixedPackage()\">"+
								"<option style=\"color:#000000\"  value=\"10\">10</option>"+
								"<option style=\"color:#000000\"  value=\"20\">20</option>"+
								"<option style=\"color:#000000\"  value=\"30\">30</option>"+
								"<option style=\"color:#000000\"  value=\"50\">50</option>"+
								"<option style=\"color:#000000\"  value=\"70\">70</option>"+
								"<option style=\"color:#000000\"  value=\"100\">100</option>"+
								"<option style=\"color:#000000\"  value=\"200\">200</option>"+
								"<option style=\"color:#000000\"  value=\"100000\">All</option>"+
							"</select>"+foor_rpp+" </td>" +
							"<td width=\"36%\" align=\"right\"><span id=\"previous\">&lt;&lt;"+foor_pre+"</span> |" +
							" <span id=\"next\">"+foor_next+"&gt;&gt;</span></td></tr>"+
							"</tbody></table></td></tr>";
				sumtext += text;
			}
	}
	$("#showList tr").not($("#showList tr").first()).remove();
	if(sumtext!=null)
		$("#showList").append(sumtext);
	$("#pageAmount").text(pageAmount);
	if(pagePointer+1 <= pageAmount){
		$("#next").attr("onclick","showFixedPackage(id)");
		$("#next").html("<a href=\"#\">Next&gt;&gt;</a>");
	}
	if(pagePointer > 1){
		$("#previous").attr("onclick","showFixedPackage(id)");
		$("#previous").html("<a href=\"#\">&lt;&lt;Previous</a>");
	}
	$("[name=maxEntries]").val(line);
	for ( var i = 0; i < pageAmount; i++) {
		$("[name=selectpage]").append(
				$("<option>").val((i+1)).html((i+1)));
		}
	$("[name=selectpage]").val(pagePointer);
	toggleCheckbox(changestatusfixed);
}

function changeStatus(id){
	var url = "/fixedpackageservice";
	var data = { func : "changeFixedPackage",
			id : id
		}
	simpleserviceWithArgs(url,data,changeStatusSuccess);
}

function changeStatusSuccess(data){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	$("#status"+objArray["id"]).text(objArray["status"]);
	closeWaitingDialog();
}

function deleteFixedPackage(){
	
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	for(var i = 0;i<$('tr [type=checkbox]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i].checked == true){
			if($($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")!=null)
				listCheckBox += $($('tr [type=checkbox]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	if(listCheckBox!=null&&listCheckBox!="")
		waitingDialog({});
	var url = "/fixedpackageservice";
	var data = { func : "delFixedPackage",
			id : listCheckBox.substring(0,listCheckBox.length - 1),
			line: line,
			pagePointer: pagePointer,
			offset: (line*pagePointer)-line
		}
	simpleserviceWithArgs(url,data,showFixedPackageList);
}

function renderEditFrom(objArray){
	$("[id*=packagedetailtable]:visible").attr("fixedId",objArray['id']);
	$("[id*=packagename]:visible").val(objArray['packagename']);
	$("[id*=description]:visible").val(objArray['description']);
	$("[id*=longofstay]:visible").val(objArray['longOfStay']);
	$("[id*=amount]:visible").val(objArray['CurrencyValue']);
	if(objArray['isForever']){
		$("[name=lengthofstay]:visible").filter("[value=forever]").prop('checked', true);
	}else{
		$("[name=lengthofstay]:visible").filter("[value=not]").prop('checked', true);
		$("[id*=from]:visible").attr("disabled",false);
		$("[id*=to]:visible").attr("disabled",false);
		$("[id*=from]:visible").val(new Date(objArray['from']).getFormatedString());
		$("[id*=to]:visible").val( new Date(objArray['to']).getFormatedString());
		$("[id*=from]:visible").attr("date",new Date(objArray['from']).toFormatString("yyyy-MM-dd"));
		$("[id*=to]:visible").attr("date",new Date(objArray['to']).toFormatString("yyyy-MM-dd"));
	}
	if($("[name=gtype]:visible").size() -1 == objArray['availableForString'].split(",").length){
		for(var i =0;i<$("[name=gtype]:visible").size();i++){
			$($("[name=gtype]:visible")[i]).attr("checked",true);
		}
	}else{
		for(var x =0;x<objArray['availableForString'].split(",").length;x++){
			for(var i =1;i<$("[name=gtype]:visible").size();i++){
				if(objArray['availableForString'].split(",")[x] == $($("[name=gtype]")[i]).val()){
					$($("[name=gtype]:visible")[i]).attr("checked",true);
					break;
				}
			}
		}
	}
	if(objArray['status']){
		$("[name=gtype_on]:visible").attr("checked",true);
	}else{
		$("[name=gtype_off]:visible").attr("checked",true);
	}
	for(var i=0;i< $("#rtList tr[name=rttable]").size();i++){
		for(var x=0;x<objArray['packageprice'].length;x++){
			if($($("#rtList tr[name=rttable]")[i]).attr("roomtypeid") == objArray['packageprice'][x]['roomtypeString']){
				$($("#rtList tr[name=rttable]")[x]).find('[name=exbed]').val(objArray['packageprice'][x]['Extrabed']);
				$($("#rtList tr[name=rttable]")[x]).find('[name=ucpp]').val(objArray['packageprice'][x]['UpchargePerson']);
				$($("#rtList tr[name=rttable]")[x]).find('[name=ppn]').val(objArray['packageprice'][x]['priceperNight']);
			}
		}
	}
}

function editpackagetime(dom,id){
	var url = "/fixedpackageservice";
	var data = { func : "getpackagetimebyid",
			id : id
		}
	simpleserviceWithArgs(url,data,fillPopUpEditTime,dom);
}
function fillPopUpEditTime(data,dom){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	renderPopUpEditTime(dom,objArray); 
}
function renderPopUpEditTime(dom,objArray){
	var dialog = editAdminDataDialog("top",dom,edittime,"/adminconsole/PriceManager/edittime_fixed.jsp","addDialog",EditTimeEventClick,loadEditTimeElementFinish,objArray);
}
function loadEditTimeElementFinish(dialog,objArray){
	dialog.find("#fixedid").attr("fixedpackage",objArray['id']);
	if(objArray['isForever']){
		dialog.find("[name=isforever]").filter("[value=true]").prop('checked', true);
		dialog.find("#datefrom").attr("disabled",true);
		dialog.find("#dateto").attr("disabled",true);
	}else{
		dialog.find("[name=isforever]").filter("[value=false]").prop('checked', true);
		dialog.find("#datefrom").attr("disabled",false);
		dialog.find("#dateto").attr("disabled",false);
		dialog.find("#datefrom").val(new Date(objArray['from']).getFormatedString());
		dialog.find("#dateto").val(new Date(objArray['to']).getFormatedString());
		dialog.find("#datefrom").attr("date",new Date(objArray['from']).toFormatString("yyyy-MM-dd"));
		dialog.find("#dateto").attr("date",new Date(objArray['to']).toFormatString("yyyy-MM-dd"));
	}
	setDatePicker();
}
function EditTimeEventClick(dialog){
	edittimefunction(dialog);
}
function edittimefunction(dialog){
	var id = dialog.find("#fixedid").attr("fixedpackage");
	if(dialog.find("[name=isforever]:checked").val() == "true"){
		var isforever = true;
		var datefrom = null;
		var dateto = null;
	}else{
		var isforever = false;
		if(dialog.find("#datefrom").val() == "" || dialog.find("#dateto").val() == ""){
			simpleDialog("please input data");
			return;
		}else{
			var datefrom = dialog.find("#datefrom").attr("date");
			var dateto = dialog.find("#dateto").attr("date");
		}
	}
	var url = "/fixedpackageservice";
	var data = { func : "editpackagetime",
			id : id,
			isforever : isforever,
			datefrom : datefrom,
			dateto : dateto
		}
	simpleserviceWithArgs(url,data,showFixedPackage,"edittime");
}

function changestatusfixed(dom,id){
	var checkspan = $("#"+id).attr('checked');
	if(checkspan==null){
		waitingDialog({});
		changeStatus(id);
	}else{
		if($("[id*=BlankDialog]:visible")[0]==null){
			var url = "/fixedpackageservice";
			var data = { func : "getpackagetimebyid",
					id : id
				}
			simpleserviceWithArgs(url,data,fillPopUpChangeStatus,dom);
		}
	}
}
function fillPopUpChangeStatus(data,dom){
	var objArray = new Array();
	objArray = JSON.parse(data);
	var checkLogOut = checkSessionAdmin(objArray);
	if(checkLogOut)
		return;
	renderPopUpChangeStatus(dom,objArray); 
}
function renderPopUpChangeStatus(dom,objArray){
	if($("[id*=BlankDialog]:visible")[0]==null){
		var obj = {"dom":dom,"objArray":objArray};
		var dialog = editAdminDataDialog("top",dom,extendTime,"/adminconsole/PriceManager/edittime_fixed.jsp","addDialog",ChangeStatusEventClick,loadChangeStatusElementFinish,obj);
	}
}
function loadChangeStatusElementFinish(dialog,objArray){
	$('input#' + objArray.objArray.id).attr('checked', 'checked');
	//dom.removeClass('iToff').addClass('iTon');
	dialog.find("#fixedid").attr("fixedpackage",objArray.objArray['id']);
	if(objArray.objArray['isForever']){
		dialog.find("[name=isforever]").filter("[value=true]").prop('checked', true);
		dialog.find("#datefrom").attr("disabled",true);
		dialog.find("#dateto").attr("disabled",true);
	}else{
		dialog.find("[name=isforever]").filter("[value=false]").prop('checked', true);
		dialog.find("#datefrom").attr("disabled",false);
		dialog.find("#dateto").attr("disabled",false);
		dialog.find("#datefrom").val(new Date(objArray.objArray['from']).getFormatedString());
		dialog.find("#dateto").val(new Date(objArray.objArray['to']).getFormatedString());
		dialog.find("#datefrom").attr("date",new Date(objArray.objArray['from']).toFormatString("yyyy-MM-dd"));
		dialog.find("#dateto").attr("date",new Date(objArray.objArray['to']).toFormatString("yyyy-MM-dd"));
	}
	setDatePicker();
}
function ChangeStatusEventClick(dialog,objArray){
	objArray.dom.removeClass('iToff').addClass('iTon');
	changestatusfunction(dialog);
}
function changestatusfunction(dialog){
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	var id = dialog.find("#fixedid").attr("fixedpackage");
	if(dialog.find("[name=isforever]:checked").val() == "true"){
		var isforever = true;
		var datefrom = null;
		var dateto = null;
	}else{
		var isforever = false;
		if(dialog.find("#datefrom").val() == "" || dialog.find("#dateto").val() == ""){
			simpleDialog("please input data");
			return;
		}else{
			var datefrom = dialog.find("#datefrom").attr("date");
			var dateto = dialog.find("#dateto").attr("date");
		}
	}
	var url = "/fixedpackageservice";
	var data = { func : "changestatustime",
			id : id,
			isforever : isforever,
			datefrom : datefrom,
			dateto : dateto,
			line: line,
			pagePointer: pagePointer,
			offset: (line*pagePointer)-line,
		}
	simpleserviceWithArgs(url,data,showFixedPackageList);
}

function toggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = true;
		}
	}else{
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}
function cleartoggleSelect(){
	if($("#showList tr [type=checkbox]")[0].checked == true){
		for(var i = 0;i< $("#showList tr [type=checkbox]").size();i++){
			$("#showList tr [type=checkbox]")[i].checked = false;
		}
	}
}

function setDatePicker(){
	$("#datefrom").datepicker({
		dateFormat: 'yy-mm-dd',
		minDate: new Date(getServerDateTime()),
		onSelect: function(DATE)
		{
			$('#datefrom').val($.datepicker.parseDate("yy-mm-dd",DATE).getFormatedString());
			$('#datefrom').attr('date',DATE);
			$( "#dateto").datepicker( "option", "minDate", DATE);
		}
	});
	$("#dateto").datepicker({
		dateFormat: 'yy-mm-dd',
		onSelect: function(DATE)
		{
			$('#dateto').val($.datepicker.parseDate("yy-mm-dd",DATE).getFormatedString());
			$('#dateto').attr('date',DATE);
		}
	});
}

function changearchived(id){
	var listCheckBox = "";
	var line = $("[name=maxEntries]").val();
	var pagePointer = $("[name=selectpage]").val();
	for(var i = 0;i<$('tr [type=checkbox][name*=ids ]').not($('tr [type=checkbox]').first()).length;i++){
		if($('tr [type=checkbox][name*=ids ]').not($('tr [type=checkbox]').first())[i].checked == true){
			if($($('tr [type=checkbox][name*=ids ]').not($('tr [type=checkbox]').first())[i]).attr("value")!=null)
				listCheckBox += $($('tr [type=checkbox][name*=ids ]').not($('tr [type=checkbox]').first())[i]).attr("value")+",";
		}
	}
	if(listCheckBox!=null&&listCheckBox!=""){
		waitingDialog({});
		var url = "/fixedpackageservice";
		var data = { func : "changeArchived",
				id : listCheckBox.substring(0,listCheckBox.length - 1),
				line: line,
				pagePointer: pagePointer,
				offset: (line*pagePointer)-line,
			}
		simpleserviceWithArgs(url,data,showFixedPackageList);
	}
}
function toggleCheckbox(func){
	var Toggle = $('[id*=itoggle]');
	for(var i =0;i<Toggle.length;i++){
		var dom = $(Toggle[i]).children();
		var id = $(Toggle[i]).children().attr("id");
		iToggle(dom,func,id); //element,success_function,id_element
	}
	closeWaitingDialog();
	//$('#itoggle input#example_1').iToggle({});
}